import { ParamArr, SQLTable } from '../../../public/js/paramarr'
import { getServerData } from '@/api/system'

const state = {
  SystemConfig: new Map(), // 对应原g_SystemConfig全局变量
  SystemConfigArr: [] // 对应原g_SystemConfig全局变量
}

const mutations = {
  setSystemConfig(state, SystemConfig) {
    state.SystemConfig = SystemConfig
  },
  setSystemConfigArr(state, SystemConfig) {
    state.SystemConfigArr = SystemConfig
  }
}

const actions = {
  getSystemConfig(state) {
    // 登录成功，设置全局变量SystemConfig， 对应原g_systemConfig
    const nodes = []
    const vPar = new ParamArr()
    vPar.setStrList('Item', ['系统设置'])
    getServerData(vPar).then(vPar => {
      const nodeMap = new Map()
      const table = new SQLTable(vPar.getTable('k_environment'))
      table.first()
      while (!table.eof()) {
        const node = {}
        node['pid'] = table.getValueByName('PID')
        node['id'] = table.getValueByName('ID')
        node['name'] = table.getValueByName('Name')
        node['value'] = table.getValueByName('Value')
        node['type'] = table.getValueByName('Type')
        node['defValue'] = table.getValueByName('DefValue')
        node['loginID'] = table.getValueByName('LoginID')
        nodes.push(node)
        nodeMap.set(node['name'], node)
        table.next()
      }
      state.commit('setSystemConfig', nodeMap)
    }).catch(() => {})
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
